import OperaBtnRender from '@/components/OperaBtnRender';
import TableRender from '@/components/TableRender';
import { handleExportExcel, addCommas } from '@/utils/utils.jsx';
import { stringify } from 'qs';

const dateFormat = 'YYYY-MM-DD HH:mm:ss';

const ExpressBillList = ({ data, query, pages, setPages, headerHeight, handleFiltersChange }) => {

  const handleReceiverAddress = (obj) => { //区分address
    if (obj.receiver_address) {
      return obj.receiver_address
    } else {
      if (obj.receiverAddress) {
        return obj.receiverAddress
      } else {
        return ''
      }
    }
  }

  const renderReciverInfo = (record) => {
    const obj = JSON.parse(record.orderReceiverInfo);
    return (
      <div>
        <span>{obj.receiverName || ''} {obj.receiverPhone || ''} </span>
        <span>{obj.province ? obj.province + '-' : ''}{obj.city ? obj.city + '-' : ''}{obj.district ? obj.district + '-' : ''}{handleReceiverAddress(obj)}</span>
      </div>
    )
  }

  const columns = [
    {
      title: "系统订单号",
      code: "orderSn",
      width: 150
    },
    {
      title: "订单类型",
      code: "orderType",
      width: 130
    },
    {
      title: "发货渠道",
      code: "channelName",
      width: 120
    },
    {
      title: "物流单号",
      code: "logisticsNo",
      width: 150
    },
    {
      title: "快递公司",
      code: "logisticsCompany",
      width: 120
    },
    {
      title: "收件信息",
      code: "receiverInfo",
      width: 200,
      render: (_, record) => {
        if (record.orderReceiverInfo) {
          return renderReciverInfo(record)
        } else {
          return ''
        }
      }
    },//1030
    {
      title: "发货时间",
      code: "deliveryTime",
      width: 150
    },
    {
      title: "实际称重重量（kg）",
      code: "weight",
      width: 180
    },
    {
      title: "计费重量（kg）",
      code: "chargeWeight",
      width: 150,
    },
    {
      title: "订单运费（元）",
      code: "feeAmount",
      width: 150,
      render: (text) => text ? addCommas(text) : 0
    },
    {
      title: "支付运费（元）",
      code: "logisticsRealFee",
      width: 150,
      render: (text) => text ? addCommas(text) : 0
    },
    {
      title: "优惠运费（元）",
      code: "disAmount",
      width: 140,
      render: (text, { feeAmount }) => Math.min(text, feeAmount)
    },
    {
      title: "称重重量（kg）",
      code: "weighingWeigh",
      width: 150,
    },
    {
      title: "计抛重量（kg）",
      code: "meterWeight",
      width: 150,
    },
    {
      title: "是否计抛计费",
      code: "isMeterBilling",
      width: 110,
    },
    {
      title: "是否为优惠快递",
      code: "isDisexpress",
      width: 130
    },
    {
      title: "店铺",
      code: "ecStoreName",
      width: 180
    }
  ];

  //导出函数
  const exportExcel = () => {
    START_LOADING();
    const params = { ...query };
    if (query.time) {// 发货时间
      params.startTime = query.time[0].format(dateFormat);
      params.endTime = query.time[1].format(dateFormat);
    }
    params.time = undefined;
    if (params.orderType == '1') {
      params.orderType = undefined;
      handleExportExcel(`/zjf-oms/payorder/expressBillsPageNewExport?${stringify(params)}`, '快递账单导出.xlsx', () => {
        END_LOADING();
      })
    } else {
      params.orderType = undefined;
      handleExportExcel(`/zjf-erp-spd/spdorder/userExpressOrdersNewExport?${stringify(params)}`, '快递账单导出.xlsx', () => {
        END_LOADING();
      })
    }
  }

  //表格组件
  const TableRenderProps = {
    data,
    columns,
    rowKey: 'orderSn',
    style: {
      height: `calc(100vh - ${headerHeight + 224}px)`
    },
    pageConfig: {
      pages,
      handlePageSize(page, size) {
        console.log(page, size)
        setPages({
          ...pages,
          current: page,
          pageSize: size
        });
      }
    }
  }

  //操作按钮
  const btnArr = [
    { elType: 'button', text: '表格导出', onClick: exportExcel }
  ]

  return (
    <div className='global-page-wrap'>
      <OperaBtnRender style={{ marginBottom: '12px' }} btnArr={btnArr} />
      <TableRender {...TableRenderProps} />
    </div>
  )
}

export default ExpressBillList;
